package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.mentor.bo.InsProjectAddBo;
import com.ruoyi.mentor.bo.InsProjectEditBo;
import com.ruoyi.mentor.bo.InsProjectQueryBo;
import com.ruoyi.mentor.domain.InsProject;
import com.ruoyi.mentor.mapper.InsProjectMapper;
import com.ruoyi.mentor.service.IInsProjectService;
import com.ruoyi.mentor.vo.InsProjectVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 导师科研项目Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class InsProjectServiceImpl extends ServiceImpl<InsProjectMapper, InsProject> implements IInsProjectService {

    @Override
    public InsProjectVo queryById(Long id){
        return getVoById(id, InsProjectVo.class);
    }

    @Override
    public TableDataInfo<InsProjectVo> queryPageList(InsProjectQueryBo bo) {
        PagePlus<InsProject, InsProjectVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), InsProjectVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<InsProjectVo> queryList(InsProjectQueryBo bo) {
        return listVo(buildQueryWrapper(bo), InsProjectVo.class);
    }

    private LambdaQueryWrapper<InsProject> buildQueryWrapper(InsProjectQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsProject> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(InsProjectAddBo bo) {
        InsProject add = BeanUtil.toBean(bo, InsProject.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(InsProjectEditBo bo) {
        InsProject update = BeanUtil.toBean(bo, InsProject.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InsProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
